﻿using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Card : MonoBehaviour
{
    public CardType cardType = CardType.NONE;
    public JsonData value;
    public int score;


    public GameObject product;
    public GameObject upgrade;
    public GameObject transaction;
    public GameObject order;

    void Start()
    {
        
    }

    public void Refresh()
    {
        this.product.SetActive(false);
        this.upgrade.SetActive(false);
        this.transaction.SetActive(false);
        this.order.SetActive(false);

        //0-生产
        if (this.cardType == CardType.PRODUCT)
        {
            SetSpice(this.product);
        }
        //1-升级
        else if (this.cardType == CardType.UPGRADE)
        {
            SetSpice(this.upgrade);
        }
        //2-交易
        else if (this.cardType == CardType.TRANSACTION)
        {
            SetSpice(this.transaction);
        }
        //3-订单
        else if (this.cardType == CardType.ORDER)
        {
            SetSpice(this.order);
        }
    }

    public void SetSpice(GameObject node)
    {
        node.SetActive(true);

        Transform content = node.transform.Find("content");
        
        for (int i = 0; i < content.transform.childCount; i++)
        {
            GameObject go = content.transform.GetChild(i).gameObject;
            go.gameObject.SetActive(false);
        }

        for (int i = 0; i < value.Count; i++)
        {
            int v = (int)value[i];

            GameObject go;
            if (i < content.transform.childCount)
            {
                go = content.transform.GetChild(i).gameObject;
            }
            else
            {
                go = new GameObject("Empty");
            }

            go.gameObject.SetActive(true);
            go.name = i.ToString();
            go.transform.SetParent(content.transform);


            //颜色
            Image image = go.GetComponent<Image>();
            if (image == null)
            {
                image = go.AddComponent<Image>();
            }
            
            if ((SpiceColor)v == SpiceColor.YELLOW)
            {
                image.color = Color.yellow;
            }
            else if ((SpiceColor)v == SpiceColor.RED)
            {
                image.color = Color.red;
            }
            else if ((SpiceColor)v == SpiceColor.GREEN)
            {
                image.color = Color.green;
            }
            else if ((SpiceColor)v == SpiceColor.COFFEE)
            {
                image.color = new Color(110f / 255f, 70f / 255f, 0f / 255f);
            }


            else if (v == -1)
            {
                Sprite sprite = ResourceManager.dictionary_sprite[("Assets/arrow_down.jpg").ToLower()];
                image.sprite = sprite;

            }
            else if (v == -2)
            {
                Sprite sprite = ResourceManager.dictionary_sprite[("Assets/arrow_up.jpg").ToLower()];
                image.sprite = sprite;
                    
            }
            else if (v == 4)
            {
                image.color = Color.gray;
            }

            //分数
            Transform go_Text_score = node.transform.Find("Text_score");
            if(go_Text_score)
            {
                Text Text_score = go_Text_score.GetComponent<Text>();
                if (Text_score != null)
                {
                    Text_score.text = this.score.ToString();
                }
            }
               
        }
    }
    

}
